cmake_minimum_required(VERSION 3.15)
project(counter_example)

set(CMAKE_CXX_STANDARD 17)

# 查找Emscripten工具链
if(NOT EMSCRIPTEN)
    message(FATAL_ERROR "This project requires Emscripten to build")
endif()

add_executable(counter counter.cpp)

if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")

    # 设置方法１
    ## 设置输出为JavaScript和Wasm
    #set_target_properties(counter PROPERTIES
    #    SUFFIX ".js"
    #    LINK_FLAGS "-s WASM=1 -s MODULARIZE=1 -s EXPORT_ES6=1 -lembind"
    #)

    # 设置方法２
    # 设置输出文件名为my_module.js (而不是默认的a.out.js)
    set_target_properties(counter PROPERTIES
        OUTPUT_NAME "counter"
        SUFFIX ".js"
    )
    
    target_link_options(counter PRIVATE
    # 启用ES6模块输出
    "SHELL:-s MODULARIZE=1"
    "SHELL:-s EXPORT_ES6=1"
    
    # 其他需要的选项，例如：
    "SHELL:-s WASM=1"
    "SHELL:-s ALLOW_MEMORY_GROWTH=1"
    "SHELL:-s ENVIRONMENT=web"
    
    # 必须包含embind库
    "SHELL:-lembind"
        
    ## 导出必要的函数
    #"SHELL:-s EXPORTED_FUNCTIONS=['_malloc','_free']"

    # 导出必要的运行时方法
    #"SHELL:-s EXPORTED_RUNTIME_METHODS=['ccall','cwrap','UTF8ToString']"
    )
    
endif()
